﻿package com.gamebase.scence
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	
	public class SenceLoader extends Loader
	{
		private var checkTimer:Timer;
		private var iniMoveStep:uint = 10;
		private var moveStep:uint = iniMoveStep;
		private var leftMoveAble:Boolean = false;
		private var rightMoveAble:Boolean = false;
		private var upMoveAble:Boolean = false;
		private var downMoveAble:Boolean = false;
		private var moveMagin:Number = 190;//距离边沿多远时开始移动地图
		private var stopMagin:Number = 350;//距离边沿多远时停止移动地图
		
		public var showScenceWidth:int;
		public var showScenceHeight:int;
		private var m_MapWidth:Number;
		private var m_MapHeight:Number;
		
		private var _roleMoveAngle:Number;
		public var m_Scene:SubScence;
		public var m_Role:DisplayObject;
		
		
		public function SenceLoader(ob:SubScence)
		{
			super();
			m_Scene = ob;
			checkTimer = new Timer(50);
			checkTimer.start();
			checkTimer.addEventListener( TimerEvent.TIMER,timerHandle );
		}
		
		/**设置场景尺寸
		 * fitStage :显示场景宽高为舞台宽高。
		 * */
		public function setScenceSize(fitStage:Boolean=false):void
		{
			if(fitStage)
			{
				showScenceWidth = this.content.stage.stageWidth;
				showScenceHeight = this.content.stage.stageHeight;
			}
			else
			{
				showScenceWidth = this.contentLoaderInfo.width;
				showScenceHeight = this.contentLoaderInfo.height;
			}
			m_MapWidth = this.content.width;
			m_MapHeight = this.content.height;
			
			masks(showScenceWidth,showScenceHeight);
		}
		
		private function masks(w:Number,h:Number):void
		{
			var _mask:Sprite = new Sprite();
			_mask.graphics.beginFill(0xff0000);
			_mask.graphics.drawRect(0,0,w,h);
			_mask.x = m_Scene.x;
			_mask.y = m_Scene.y;
			m_Scene.parent.addChild(_mask);
			m_Scene.mask = _mask;
		}
				
		//检查移动方向
		public function checkSenceMove(roleMoveAngle:Number,rolePoint:Point):void{
			if(this.content.stage == null) return;
			
			_roleMoveAngle = roleMoveAngle*100;
			
			//右移
			if( rolePoint.x<moveMagin)
			{
				if((_roleMoveAngle>=-313 && _roleMoveAngle<-157) || (_roleMoveAngle>158 && _roleMoveAngle<315))
				{
					this.rightMoveAble = true;
					this.leftMoveAble = false;
					moveStep = iniMoveStep;
					if(!checkTimer.running)
						checkTimer.start();
				}
			}
			
			//左移
			if(rolePoint.x > (showScenceWidth-moveMagin))
			{
				if(_roleMoveAngle>-158 && _roleMoveAngle<157)
				{
					this.leftMoveAble = true;
					this.rightMoveAble = false;
					moveStep = iniMoveStep;
					if(!checkTimer.running)
						checkTimer.start();
				}
			}
			
			//上移
			if(rolePoint.y > (showScenceHeight - moveMagin) )
			{
				if(_roleMoveAngle>0 && _roleMoveAngle<=314)
				{
					this.upMoveAble = true;
					this.downMoveAble = false;
					moveStep = iniMoveStep;
					if(!checkTimer.running)
						checkTimer.start();
				}
			}
			
			//下移
			if(rolePoint.y < moveMagin)
			{
				if(_roleMoveAngle>-314 && _roleMoveAngle<0)
				{
					this.downMoveAble = true;
					this.upMoveAble = false;
					moveStep = iniMoveStep;
					if(!checkTimer.running)
						checkTimer.start();
				}
			}
			
		}
		
		private function timerHandle( evt:TimerEvent ):void{
			if(!m_Role)
				return;

			if(leftMoveAble)
			{
				if((-m_Scene.referX + this.m_Role.x + this.m_Scene.x) < (showScenceWidth-stopMagin))
					leftMoveAble = false;
				if(-m_Scene.referX + m_Scene.x-moveStep < showScenceWidth-/*m_Scene["m_MapWidth"]*/m_MapWidth)
				{
					m_Scene.x = showScenceWidth-/*m_Scene["m_MapWidth"]*/m_MapWidth + m_Scene.referX;
					leftMoveAble = false;
				}
				else
				{
					if(moveStep>3)	moveStep-=2;
					m_Scene.x -= moveStep;
				}
					
			}
			if(rightMoveAble)
			{
				if((-m_Scene.referX + this.m_Role.x + this.m_Scene.x) > stopMagin)
					rightMoveAble = false;
				if(-m_Scene.referX + m_Scene.x+moveStep > 0)
				{
					m_Scene.x = m_Scene.referX;
					rightMoveAble = false;
				}
				else
				{
					if(moveStep>3)	moveStep-=2;
					m_Scene.x += moveStep;
				}
			}
			if(upMoveAble)
			{
				if((-m_Scene.referY + this.m_Role.y + this.m_Scene.y) < (showScenceHeight -stopMagin) )
					upMoveAble = false;
				if(-m_Scene.referY + m_Scene.y-moveStep < showScenceHeight-/*m_Scene["m_MapHeight"]*/m_MapHeight)
				{
					m_Scene.y = showScenceHeight-/*m_Scene["m_MapHeight"]*/m_MapHeight + m_Scene.referY;
					upMoveAble = false;
				}
				else
				{
					if(moveStep>3)	moveStep-=2;
					m_Scene.y -= moveStep;
				}
					
			}
			if(downMoveAble)
			{
				if((-m_Scene.referY + this.m_Role.y + this.m_Scene.y) >stopMagin)
					downMoveAble = false;
				if(-m_Scene.referY + m_Scene.y+moveStep > 0)
				{
					m_Scene.y = m_Scene.referY;
					downMoveAble = false;
				}
				else
				{
					if(moveStep>3)	moveStep-=2;
					m_Scene.y += moveStep;
				}
					
			}
			
			if(!(leftMoveAble || rightMoveAble || upMoveAble || downMoveAble))
			{
				this.checkTimer.stop();
			}
		}
		/* 释放此类中的所有资源*/
		public function releaseRes():void{  
			 checkTimer.removeEventListener( TimerEvent.TIMER ,timerHandle );  
		}
		
	}
}